﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 29/12/2009
 * Time: 9:55
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Purchase.
	/// </summary>
	public class Purchase : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region Declaration variables
			private String _number_purchase; 
			private String _serie; 
			private String _document; 
			private String _date_emissao; 
			private String _date_entry; 
			private String _supplier; 
			private String _type_move; 		
			private String _ipi_total; 
			private String _discount_total; 
			private String _general_total; 
			private String _type_move_id;  
			private String _company_id;
			private String _user_id;
		#endregion
		
		public Purchase()
		{
			clear();
			
			gsql.table = "common.purchase";
			_sequence  = "common.purchase_purchase_id_seq";
			_company_id  = Information.companyId;
			_user_id   = Information.userId;
		}
		
		#region Declaration methods Gets end Sets
			public string Number_purchase {
				get { return _number_purchase; }
				set { _number_purchase = value; }
			}
			
			public string Serie {
				get { return _serie; }
				set { _serie = value; }
			}
			
			public string Document {
				get { return _document; }
				set { _document = value; }
			}
			
			public string Date_emissao {
				get { return _date_emissao; }
				set { _date_emissao = value; }
			}
			
			public string Date_entry {
				get { return _date_entry; }
				set { _date_entry = value; }
			}
			
			public string Supplier {
				get { return _supplier; }
				set { _supplier = value; }
			}
			
			public string Type_move {
				get { return _type_move; }
				set { _type_move = value; }
			}
			
			public string Ipi_total {
				get { return _ipi_total; }
				set { _ipi_total = value; }
			}
			
			public string Discount_total {
				get { return _discount_total; }
				set { _discount_total = value; }
			}
			
			public string General_total {
				get { return _general_total; }
				set { _general_total = value; }
			}
			
			public string Type_move_id {
				get { return _type_move_id; }
				set { _type_move_id = value; }
			}
		
			public string Company_id {
				get { return _company_id; }
				set { _company_id = value; }
			}
			
			public string User_id {
				get { return _user_id; }
				set { _user_id = value; }
			}
			
		#endregion
		
		public void clear()
		{
			_id 					= String.Empty;
			_number_purchase        = String.Empty;
			_serie                  = String.Empty;
			_document				= String.Empty; 
			_date_emissao 			= String.Empty;
			_date_entry 			= String.Empty;
			_supplier 				= String.Empty;
			_type_move 				= String.Empty;
			_ipi_total 				= String.Empty;
			_discount_total 		= String.Empty;
			_general_total 			= String.Empty;
			_type_move_id 			= String.Empty;
			_user_id   				= Information.userId;
			_company_id 			= Information.companyId;
		}
		
		public override bool delete()
		{
			return base.delete();
		}
		
		public override void get(System.Data.DataRow dr)
		{
			_id                    	= dr["purchase_id"].ToString();
			_number_purchase       	= dr["purchase_number_purchase"].ToString();
			_serie                 	= dr["purchase_serie"].ToString();
			_document              	= dr["purchase_document"].ToString();
			_date_emissao          	= Convert.ToDateTime(dr["purchase_date_emissao"]).ToString("dd/MM/yyyy");
			_date_entry            	= Convert.ToDateTime(dr["purchase_date_entry"]).ToString("dd/MM/yyyy");
			_supplier              	= dr["purchase_supplier"].ToString();
			_ipi_total  			= dr["purchase_ipi_total"].ToString();
			_discount_total  		= dr["purchase_discount_total"].ToString();
			_general_total  		= dr["purchase_general_total"].ToString();
			_type_move_id 	 		= dr["purchase_type_move_id"].ToString();
			_company_id 			= dr["purchase_company_id"].ToString();
			_user_id				= dr["purchase_user_id"].ToString();
		}
		
		public override bool save()
		{
			return base.save();
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id                  	                                   , "purchase_id");
			gsql.setValue(_number_purchase     	                                   , "purchase_number_purchase");
			gsql.setValue(_serie               	                                   , "purchase_serie");
			gsql.setValue(_document            	                                   , "purchase_document");
			gsql.setValue(Convert.ToDateTime(_date_emissao).ToString("yyyy/MM/dd") , "purchase_date_emissao");
			gsql.setValue(Convert.ToDateTime(_date_entry).ToString("yyyy/MM/dd")   , "purchase_date_entry");
			gsql.setValue(_supplier            	                                   , "purchase_supplier");
			gsql.setValue(_ipi_total  				                               , "purchase_ipi_total");
			gsql.setValue(_discount_total  		                                   , "purchase_discount_total");
			gsql.setValue(_general_total  		                                   , "purchase_general_total");
			gsql.setValue(_type_move_id 	 		                               , "purchase_type_move_id");
			gsql.setValue(_company_id 				                               , "purchase_company_id");
			gsql.setValue(_user_id				                                   , "purchase_user_id");
		}
		
		public override bool store(csharp.common.GenerateSql.TCommandType ctype)
		{
			return base.store(ctype);
		}
		
		public override bool update()
		{
			return base.update();
		}
		
		public void changeTableToTemp()
		{
			gsql.table = "common.purchase_temp";
			_sequence = "common.purchase_temp_purchase_temp_id_seq";
		}
		
		public void changeTableToMain()
		{
			gsql.table = "common.purchase";
			_sequence = "common.purchase_purchase_id_seq";
		}
		
	}
	
	public class Purchases : csharp.common.DefaultList, csharp.common.IList
	{
		public Purchases()
		{
			gsql.table = "common.purchase";
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "purchase_company_id = "+Information.companyId;
			//TODO: Verificar depois
			//gsql.conditionalWhere = "purchase_user_id = '"+Information.userId+"'";
			gsql.additionalWhere = "ORDER BY purchase_date_emissao";
			
			return mountList();
		}
		
		public bool fillList(string purchase_number, string doc_number, string init_date, string end_date)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "purchase_company_id = "+Information.companyId;
			//TODO: Verificar depois
			//gsql.conditionalWhere = "purchase_user_id = '"+Information.userId+"'";
			
			gsql.conditionalWhere = "purchase_number_purchase LIKE '%"+purchase_number+"%'";
			gsql.conditionalWhere = "purchase_document LIKE '%"+doc_number+"%'";
			gsql.conditionalWhere = "(purchase_date_emissao BETWEEN '"+Convert.ToDateTime(init_date).ToString(common.CONSTANTS.dateFormatInternational)
															+"' AND '"
																	+Convert.ToDateTime(end_date).ToString(common.CONSTANTS.dateFormatInternational)+"')";
			gsql.additionalWhere = "ORDER BY purchase_date_emissao";
			
			return mountList();
		}		
		
		public Purchase get(Int32 index)
		{
			return (Purchase)items[index];
		}
		
		public Purchase get()
		{
			return (Purchase)items[index];
		}
		
		public bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			items.Clear();
			tam= gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				Purchase p = new Purchase();
				p.get(gsql.getRow(i));
				items.Add(p);
			}
			return true;
		}
	}
}
